/**
 *  ajax分页类
 *  调用方法 pages = new ajaxPage( obj );
 *	构造方法
 *		ajaxPage( obj )
 *		参数:obj为js对象,对象属性如下
 *			url				请求地址(必填)
 *			rows			每页显示记录数,默认为10
 *			pages			分页显示列数,默认为10
 *			pageDiv			分页Html渲染div
 *			setParams		设置条件参数
 *			successCallFun	请求成功后回调方法(必填)
 *
 *	方法(以下方法执行后,会重新渲染分页样式):
 *		setUrl		设置请求地址
 *		setPages	设置显示分页列数
 *		setRows		设置每页记录行数
 *		
 *	调用示例
 *		包含 /css/page.css		分页样式
 *		包含 /js/ajax_pages.js	js分页文件
 *
 *		page = new ajaxPages({
 *			url				: '',	//必填项
 *			rows			: '',	//可选
 *			pages			: '',	//可选
 *			pageDiv			: '',	//必填项
 *			setParams		: 'getConditions',	//可选
 *			successCallFun	: function(result){		//必填项
 *				result['result']
 *			}
 *		})
 *
 *		function getConditions(){
 *			var _userName = $('#user_name').val();
 *			var _age	  = $('#age').val();
 *			return 'user_name=' + _userName + '&age=' + _age;
 *		}
 *
 *	服务器代码
 *		ajax会自动post以下数据
 *		$_POST['currpage']	//当前页
 *		$_POST['rows']		//每页显示记录数
 *	在处理过程中,应该适当判断rows值,如果rows非常大,应该默认值,最好和客户端向匹配
 *
 *
 *	服务端实现数据格式
 *		返回数据需为json格式,格式必须包含以下属性
 *			rows	不能修改,总记录数
 *			result	为自定义属性，推荐统一用result数据为任意格式比如(数据、html代码)等，建议为纯数据
 *	示例
 *		echo json_encode(
 *			array(
 *				'rows'	=> 100,
 *				'result	=> array(
 *					0=>array(
 *						'id'	=> 1,
 *						'name'	=> 'name1',
 *					)
 *					2=>array(
 *						'id'	=> 2,
 *						'name'	=> 'name2',
 *					)
 *				)
 *			)
 *		);
 **/

function ajaxPage( obj ){
	//请求url地址,数据请求地址
	if(obj.url == undefined){
		alert('url地址未定义');
		return;
	}else{
		this._url		= obj.url;
	}
   
	//每页显示记录数
	if(obj.rows == undefined){
		this._rows		= 10;
	}else{
		this._rows		= obj.rows;
	}
	//分页列数
	if(obj.pages == undefined){
		this._pages		= 10;
	}else{
		this._pages		= obj.pages;
	}

	//总页数
	if(obj.allrows == undefined){
		this._allrows		= 1;
	}else{
		this._allrows		= obj.allrows;
	}

	//当前页数
	if(obj.currPage == undefined){
		this._currPage		= 1;
	}else{
		this._currPage		= obj.currPage;
	}

	//分页需要渲染的div
	if(obj.pageDiv == undefined){
		alert('分页需要渲染的div未定义');
		return;
	}else{
		this._pageDiv		= obj.pageDiv;
	}

	//请求的条件字符串
	this._data = '';
	if(obj.setParams != undefined){
		this._setParam = obj.setParams;
	}
	
	//请求成功后，回调方法
	if(obj.successCallFun == undefined){
		alert('成功后回调函数未定义');
		return;	
	}else{
		this._successCallFun = obj.successCallFun;
	}

	this._initAjaxRequest();
}


//设置url
ajaxPage.prototype.setUrl = function( url ){
	this._url		= url;
	this._initAjaxRequest();
}

//设置列
ajaxPage.prototype.setPages = function( pages ){
	this._pages		= pages;
	this._initAjaxRequest();
}

//设置记录数
ajaxPage.prototype.setRows = function( rows ){
	this._rows		= rows;
	this._initAjaxRequest();
}


//创建分页html代码
ajaxPage.prototype._createPapeHtml = function(){

	var allrows, colss, rows, page ,div1, fun;
	
	allrows = this._allrows;
	colss	= this._pages;
	rows	= this._rows;
	page	= this._currPage;
	div1	= this._pageDiv;
	var rowscnt = allrows;
	var ii = allrows / rows ;
	if(allrows%rows!=0){
		allrows = parseInt(ii) + 1;
	}
	else{
		allrows = ii;
	}

	/*默认显示列数*/
	if( colss=='') colss = 6;

	/*取列数中间值*/
	var cols = 0;

	if(colss%2!=0){
		cols= (colss+1)/2;
	}else{
		cols = colss/2;
	}
	
	/*获取URL参数*/
	var url = document.location.href;

	/*获取没有页面参数的URL，如果URL中有页面参数，只要前面部分的*/
	var i = 0;

	if(parseInt(page)=='NaN'){
		var cpages=1;
	}else{
		cpages = parseInt(page);
	}
	
	var epages = cpages + cols;
	var spages = cpages - cols + 1;
	
	if (spages <=1 ){
		spages = 1;
		epages = colss+1;
	}
	if (epages >allrows ){
		spages = allrows-colss+1;
		epages = allrows+1;	
	}
	if(allrows<colss){
		epages = allrows+1;
	}
	if(spages<1){
		spages=1;
	}
	
	//记录数为0时
	if(allrows==0){
		document.getElementById(div1).innerHTML = '';
		return;
	}

	/*生成页字符串*/

	var pagetext ='';
    var pagetext = '<div class = "fd-right" id="pagination-digg">';

	if(parseInt(page)>1){
		var perv = parseInt(page)-1;
		pagetext +=  '<a class="prev" href="#comments_maodian" page="1">首页</a> ';
		pagetext +=  '<a class="prev" href="#comments_maodian" page="' + perv +'">上一页</a> ';
	}
    else{
		pagetext +=  '<span class="prev-disabled">首页</span> ';
		pagetext +=  '<span class="prev-disabled">上一页</span> ';

	}


	for(spages;spages<epages;spages++)
	{
		var sp = Math.ceil(spages);
		if ( sp == page )
		{
			pagetext += '<a class="page-current" href="#comments_maodian" page="' + sp + '">' + page +'</a>';
		}
		else
		{
			pagetext += ( "<a href='#comments_maodian' page='" + sp + "'>"+Math.ceil(spages)+"</a>" );
		}
	}

	if(parseInt(page) < allrows){
		var next = parseInt(page)+1;
		pagetext += '<a class="next" href="#comments_maodian" page="' + next + '">下一页</a>'
		pagetext += '<a class="next" href="#comments_maodian" page="' + allrows + '">尾页</a>'
	}else{
		pagetext +=  '<span class="prev-disabled">下一页</span> ';
		pagetext +=  '<span class="prev-disabled">尾页</span> ';
	}

	//pagetext +=  '<input type="text" class="prev-input" id="input_pagenumber" >';
	//pagetext +=  '<a class="go" href="#" go="go">跳转</a>';
	//pagetext +=  '总页数' + allrows + '&nbsp;总记录数:' + rowscnt;
	pagetext +='</div>'
	
	
	document.getElementById(div1).innerHTML = pagetext;
	this.clickPage();

}
//分页点击执行代码
ajaxPage.prototype.clickPage = function(){
	var ithis = this;
	$("#pagination-digg a").click(function(){
		var _go = $(this).attr('go');
		if(_go == undefined){
			ithis._currPage = $(this).attr('page');
		}else{
			var _p = $('#input_pagenumber').val();
			if(_p == undefined || _p == ''){
				alert('请输入页码');
				return;
			}
			ithis._currPage = _p;
		}
		
		ithis._ajaxRequest();
	});	
	//alert($("#pagination-digg a").attr('page'));
}

ajaxPage.prototype._initAjaxRequest = function(){
	this._currPage = 1;
	this._ajaxRequest();
}

ajaxPage.prototype._ajaxRequest = function(){
	this._setParams();
	var ithis = this;
	$.ajax({
		type: "POST",
		data: ithis._data ,
		cache: false,
		url: ithis._url,
		dataType : 'json',
		timeout: 3000,
		success : function(result){
			ithis._allrows	= result.rows;
			ithis._createPapeHtml();
			ithis._successCallFun(result);
		}
	});
}

//设置ajax请求的参数
ajaxPage.prototype._setParams = function(){
	this._data = 'currpage=' + this._currPage + '&rows=' + this._rows + '&';
	if(this._setParam != undefined && this._setParam !=''){
		this._data += eval(this._setParam+'()');
	}
	return this._data;
}
//刷新
ajaxPage.prototype.refresh = function(){
	this._currPage = 1;
	this._ajaxRequest();
}